package org.colcon.web.model.vote;

import java.util.ArrayList;
import java.util.List;

import org.colcon.web.model.decision.NullDecision;
import org.colcon.web.model.framework.IBallot;
import org.colcon.web.model.framework.IDecision;

public abstract class BooleanVote extends MultiChoiceVote
{
	private static final long serialVersionUID = 8344932604318527159L;
	
	public BooleanVote(String group, String id, int length, int quorum, String[] titles, IDecision trueDecision, IDecision falseDecision) 
	{
		super(group, id, length, quorum, titles, new IDecision[]{trueDecision, falseDecision});
	}
	
	public BooleanVote(String group, String id, int length, int quorum, String[] titles, IDecision trueDecision)
	{
		this(group, id, length, quorum, titles, trueDecision, new NullDecision(group));
	}
	
	public IDecision getDecision()
	{
		if(getBoolean())
			return getChoices()[0];
		else
			return getChoices()[1];
	}
	
	public int getVotes(boolean value)
	{
		int votes = 0;
		
		for(Boolean b : getVotes())
			if(b == value)
				votes++;
		
		return votes;
	}
	
	public List<Boolean> getVotes()
	{
		List<Boolean> votes = new ArrayList<Boolean>();
		
		for(IBallot record : getBallots())
		{
			String value = record.getValue();
			if(value.equals("true"))
				votes.add(true);
			else if(value.equals("false"))
				votes.add(false);
		}
		
		return votes;
	}
	
	public abstract boolean getBoolean();
}
